﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace ModelValidation.LongitudinalModel
{
	public class UnjoinElementsController
	{
		public UnjoinElementsController(ExternalCommandData cmdData)
		{
			this.m_uiApp = cmdData.Application;
			this.m_uiDoc = this.m_uiApp.ActiveUIDocument;
			this.m_app = this.m_uiApp.Application;
			this.m_doc = this.m_uiDoc.Document;
		}

		public Result UnjoinElements(ref string msg)
		{
			if (!this.GetModelsByUserSelect())
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			this.UnjoinElementsByType(this.m_Beams);
			this.UnjoinElementsByType(this.m_StructColumns);
			this.UnjoinElementsByType(this.m_StructWalls);
			this.UnjoinElementsByType(this.m_Floors);
			this.UnjoinElementsByType(this.m_ArchColumns);
			this.UnjoinElementsByType(this.m_ArchWalls);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool UnjoinElementsByType(List<Element> elements)
		{
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.m_doc);
				transaction.Start("unjoin elemet");
				foreach (Element element in elements)
				{
					foreach (ElementId elementId in JoinGeometryUtils.GetJoinedElements(this.m_doc, element))
					{
						Element element2 = this.m_doc.GetElement(elementId);
						this.UnjoinElementPair(element, element2);
					}
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (transaction != null && transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			return true;
		}

		private void UnjoinElementPair(Element elementFirst, Element elementSecond)
		{
			if (JoinGeometryUtils.AreElementsJoined(this.m_doc, elementFirst, elementSecond))
			{
				JoinGeometryUtils.UnjoinGeometry(this.m_doc, elementFirst, elementSecond);
			}
		}

		private bool GetModelsByUserSelect()
		{
			IList<Reference> list = null;
			Selection selection = this.m_uiDoc.Selection;
			try
			{
				list = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new ModelsFilter(this.m_doc), "选择取消连接的实体");
			}
			catch (Exception)
			{
				return false;
			}
			foreach (Reference reference in list)
			{
				Element element = this.m_doc.GetElement(reference);
				if (element.GetType() == typeof(Wall))
				{
                    if ((int)(element as Wall).StructuralUsage == 1)
					{
						this.m_StructWalls.Add(element);
					}
					else
					{
						this.m_ArchWalls.Add(element);
					}
				}
				else if (element.GetType() == typeof(Floor))
				{
					this.m_Floors.Add(element);
				}
				else if (element.Category.Id.IntegerValue == -2001320)
				{
					this.m_Beams.Add(element);
				}
				else if (element.Category.Id.IntegerValue == -2001330)
				{
					this.m_StructColumns.Add(element);
				}
				else if (element.Category.Id.IntegerValue == -2000100)
				{
					this.m_ArchColumns.Add(element);
				}
			}
			return true;
		}

		private UIApplication m_uiApp;

		private UIDocument m_uiDoc;

		private Application m_app;

		private Document m_doc;

		private List<Element> m_Beams = new List<Element>();

		private List<Element> m_StructColumns = new List<Element>();

		private List<Element> m_StructWalls = new List<Element>();

		private List<Element> m_Floors = new List<Element>();

		private List<Element> m_ArchColumns = new List<Element>();

		private List<Element> m_ArchWalls = new List<Element>();
	}
}
